<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use service\SmsService;
use think\facade\Config;
use think\Db;

class Sms extends Common
{

    protected $table = 'sms_';



    public function index()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            foreach ($data as $key => $value) {
                sysConfig($key, $value);
            }
            return ['ret' => '保存成功'];
        }
        $sms_platform = Config::get('sms_platform');
        $this->assign('sms_platform', $sms_platform);
        return $this->fetch();
    }

    public function template()
    {
        $platform = sysConfig('sms_platform');
        if (empty($platform)) $platform = Config::get('sms_platform')[0]['value'];
        $data = Db::name($this->table . 'template')->where('platform', $platform)->paginate();
        $this->assign('data', $data);
        $this->assign('type',SmsService::SMS_TYPE);
        return $this->fetch();
    }

    public function templateDel()
    {
        if ($this->request->isAjax()) {
            $id = input('post.id/d');
            if (!$id) {
                return ['err' => '参数错误'];
            }
            if (!Db::name($this->table . 'template')->value('id')) {
                return ['err' => '模板不存在'];
            }
            if (Db::name($this->table . 'template')->delete($id)) {
                return ['err' => '删除成功'];
            }
            return ['ret' => '删除失败，请重试'];
        }
    }

    public function templateAdd()
    {
        $config_platform = Config::get('sms_platform');
        $platform = sysConfig('sms_platform');
        if (empty($platform)) $platform = $config_platform[0]['value'];
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            if ($data['type'] < 0) {
                return ['err' => '请选择类型'];
            }
            if ($platform == $config_platform[0]['value']) {
                if (empty($data['template_id'])) {
                    return ['err' => '请输入模板ID'];
                }

                if (empty($data['template_value'])) {
                    return ['err' => '参数至少1个'];
                }

                if (!is_array($data['template_value'])) {
                    return ['err' => '参数类型错误'];
                }
                $tempVal = array();
                $flag = false;
                foreach ($data['template_value'] as $item) {
                    if (empty($item['key']) || empty($item['value'])) {
                        $flag = true;
                        break;
                    }
                    $tempVal[$item['key']] = $item['value'];
                }
                if ($flag || !count($tempVal)) {
                    return ['err' => '参数键或值为空', 'msg' => $data['template_value']];
                }
                $data['template_value'] = json_encode(trimValue($tempVal));
            } else {
                $data['template_id'] =SmsService::SMS_TYPE[$data['type']]['name'];
                $data['template_value'] = trimValue($data['template_value']);
            }
            $data['platform'] = $platform;
            $tid = Db::name($this->table . 'template')->where(['type' => $data['type'], 'platform' => $platform])->value('id');
            if ($tid && $tid != $data['id']) {
                return ['err' => '模板已存在，请删除或重新修改'];
            }
            $id = intval($data['id']);
            unset($data['id']);

            $data['update_time'] = time();
            if ($id > 0) {
                $isSave = Db::name($this->table . 'template')->where('id', $id)->update($data);
            } else {
                $data['create_time'] = $data['update_time'];
                $isSave = Db::name($this->table . 'template')->insert($data);
            }
            if (!$isSave) {
                return ['err' => '保存失败，请重试'];
            }
            return ['ret' => '保存成功'];
        }
        $tempVal = $platform == $config_platform[0]['value'] ? array() : null;
        if (input('?get.id')) {
            $data = Db::name($this->table . 'template')->where('id', input('get.id/d'))->find();
            if ($platform == $config_platform[0]['value']) {
                $data['template_value'] = json_decode($data['template_value'], true);
                foreach ($data['template_value'] as $key => $value) {
                    $tempVal[] = ['key' => $key, 'value' => $value];
                }
            } else {
                $tempVal = $data['template_value'];
            }
            $this->assign('data', $data);
        }
        $created_type = Db::name($this->table . 'template')->where('platform', $platform)->column('type');
        $this->assign('created_type', $created_type);
        $this->assign('type', SmsService::SMS_TYPE);
        $this->assign('tempVal', $tempVal);
        if ($platform == $config_platform[0]['value']) {
            return $this->fetch();
        }
        return $this->fetch('template_add2');
    }

    public function log()
    {
        $data = Db::name($this->table . 'log')->order('id desc')->paginate();
        $platform = Config::get('sms_platform');
        $this->assign('platform', array_column($platform, null, 'value'));
        $this->assign('data', $data);
        $this->assign('type', SmsService::SMS_TYPE);
        return $this->fetch();
    }
}
